package modelo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;



public class Modelo extends Conexion{
	
	public Object[][] buscarTotaldeItems() {
        Object[][] res = this.select(
        		"inventario",         		
        		" idItem, Item, Color, Talla, Logo" +
        		", Observacion, StockMinimo, EnStock, Vendidas, CostoTotal" +
        		", VentaTotal, GananciaTotal, Ingresadas"
        		, null);
        if (res.length > 0) {
        	System.out.println("Query OK");
            return res;
            
        } else {
        	System.out.println("Query null");
            return null;
        }
    }
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public DefaultComboBoxModel buscarItems() {
		DefaultComboBoxModel cbxModel = new DefaultComboBoxModel();		
		cbxModel.addElement(" ");
		Statement st;		
		try {
			st = getConnection().createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM inventario");
			while (rs.next()) {	
				cbxModel.addElement(rs.getString(1)+ "- " + rs.getString(2) + " " + rs.getString(3)+ " " + rs.getString(4)+ " "+ rs.getString(5));
			}
			rs.close();		
		} catch (SQLException e) {
			e.printStackTrace();
		}				
		return cbxModel;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public DefaultComboBoxModel buscarCentroCosto() {
		DefaultComboBoxModel cbxModel = new DefaultComboBoxModel();		
		Statement st;		
		try {
			st = getConnection().createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM centrodecosto");
			while (rs.next()) {						
				cbxModel.addElement(rs.getObject(1));
			}
			rs.close();		
		} catch (SQLException e) {
			e.printStackTrace();
		}				
		return cbxModel;
		
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public DefaultComboBoxModel buscarItemsId() {
		DefaultComboBoxModel cbxModel = new DefaultComboBoxModel();		
		cbxModel.addElement("-");
		Statement st;		
		try {
			st = getConnection().createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM inventario");
			while (rs.next()) {	
				cbxModel.addElement(rs.getObject(1));
			}
			rs.close();		
		} catch (SQLException e) {
			e.printStackTrace();
		}				
		return cbxModel;
	}

	public void ingresarStock(String txtItem, String txtCantidad,

			String txtCentroCosto, String txtCostoUnidad, String txtCostoTotal, String txtObs) {
		
		if (this.insert("ingresos", "Item, Cantidad, CostoTotal, CostoUnidad, CentroDeCosto_idCentroCosto, Observaciones", 
				 		" '" + txtItem + "','" + txtCantidad + "','" + txtCostoTotal + "','" + txtCostoUnidad + "','" + txtCentroCosto + "','" + txtObs + "' ")) {
			JOptionPane.showMessageDialog(null, "Stock ingresado al inventario");
		} else {
			JOptionPane.showMessageDialog(null, "Error al ingresar Stock", "Aviso", JOptionPane.ERROR_MESSAGE);
		}		
	}
	
	public Object[][] buscarCostoUnidadXId(String id){
		return this.select("inventario", "CostoTotal, Ingresadas", "idItem = '"+id +"'");
	}
	
	public Object[][] buscarEnStockXId(String id){
		return this.select("inventario", "EnStock", "idItem = '"+id +"'");
	}
	
	public String buscarItemXId(String id){
		String x = new String();
		Statement st;		
		try {
			st = getConnection().createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM inventario WHERE idItem = '"+ id + "'");
			while (rs.next()) {	
				x = rs.getString(1)+ "- " + rs.getString(2) + " " + rs.getString(3)+ " " + rs.getString(4)+ " "+ rs.getString(5);
//				cbxModel.addElement(rs.getString(1)+ "- " + rs.getString(2) + " " + rs.getString(3)+ " " + rs.getString(4)+ " "+ rs.getString(5));
			}
			rs.close();		
		} catch (SQLException e) {
			e.printStackTrace();
		}				
		return x;
			
//		return this.select("inventario", "EnStock", "idItem = '"+id +"'");
	}
	
	public void ingresarVenta(String txtItemId, String txtCantidad,
			String txtCostoUnidad, String txtGanancia, String txtVentaUnidad,
			String txtTotalVenta, String txtCentroCosto, String txtObs) {
		if(this.insert("ventas", "Item, Cantidad, CostoUnidad, Ganancia, VentaUnidad, TotalVenta, CentroDeCosto_idCentroCosto, Observaciones", 
				" '"+txtItemId+"','"+ txtCantidad+"','"+txtCostoUnidad+"','"+txtGanancia+"','"+txtVentaUnidad+"','"+txtTotalVenta+"','"+txtCentroCosto+"','"+txtObs+"' ")){
			JOptionPane.showMessageDialog(null, "Venta realizada, stock actualizado");
		}else {
			JOptionPane.showMessageDialog(null, "Error al ingresar Stock", "Aviso", JOptionPane.ERROR_MESSAGE);
		}			
	}

	public Object[][] buscarItemConStock() {
		return this.select("inventario", "idItem, Item, Color, Talla, Logo, Observacion, EnStock", "EnStock > 0");		
	}
	
	public Object[][] buscarIngresosXId(String id){
		return this.select("ingresos", "FechaIngreso, Cantidad, CostoTotal, CostoUnidad, Observaciones", "Item = '"+ id + "'");
	}
	
	public Object[][] buscarVentasXId(String id){
		return this.select("ventas", "FechaVenta, Cantidad, CostoUnidad, Ganancia, VentaUnidad, TotalVenta, Observaciones", "Item = '"+ id + "'");
	}
	
	public Object[][] buscarInfoCCosto() {
		return this.select("centrodecosto", "Capital, Costos, Ventas, Ganancias", null);
		
	}

	public void ingresarSolicitud(String txtItem, String txtCantidad,
			String txtSolicitante, String txtObs) {
		if(this.insert("pendientes", "Item, Cantidad, Solicitante, Observacion", 
				" '"+txtItem+"','"+ txtCantidad+"','"+txtSolicitante+"','"+txtObs+"' ")){
			JOptionPane.showMessageDialog(null, "Solicitud ingresada");
		}else {
			JOptionPane.showMessageDialog(null, "Error al ingresar Stock", "Aviso", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	public Object[][] buscarSolicitudes(String st) {
		return this.select("pendientes", "idPendientes, Item, Cantidad, Solicitante, FechaSolicitud, Observacion, Status", "Status = '"+ st +"'");		
	}
	
	public void actualizarSolicitud(String id, String st){
		this.Update("pendientes", "Status", st,"idPendientes = '"+id+"'");
	}

	public void ingresarCompra(String descrip, String costo, String centroCos) {
		if(this.insert("compras", "Descripcion, CostoCompra, centrodecosto_idCentroCosto", "'"+descrip+"', '"+ costo +"', '"+centroCos+"'")){
			JOptionPane.showMessageDialog(null, "Registro agregado");
		}else {
			JOptionPane.showMessageDialog(null, "Error al agregar registro", "Aviso", JOptionPane.ERROR_MESSAGE);
		}
	}

	public Object[][] actualizarTablaCompra() {
		return this.select("compras", "idCompras, Descripcion, CostoCompra", null);		
	}
}
